package com.cauli.file.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Cauli
 * @date 2023/9/2 15:28
 * @description RabbitMQ配置类
 */
@Configuration
public class RabbitMQConfig {
    // 普通交换机
    public static final String FILE_EXCHANGE = "file.exchange";

    // 文件保存相关
    public static final String QUEUE_FILE_SAVE = "queue.file.save";
    public static final String KEY_FILE_SAVE = "key.file.save";

    // 文件删除相关
    public static final String QUEUE_FILE_REMOVE = "queue.file.remove";
    public static final String KEY_FILE_REMOVE = "key.file.remove";

    // 死信相关
    public static final String DEAD_LETTER_EXCHANGE = "deadLetter.exchange";
    public static final String DEAD_LETTER_QUEUE = "deadLetter.queue";
    public static final String KEY_FILE_DEAD_LETTER = "key.file.dead.letter";

    //延迟队列
    public static final String DELAY_QUEUE = "delay.queue";

    /**
     * 文件保存队列
     *
     * @return
     */
    @Bean
    public Queue queueFileSave() {
        return new Queue(QUEUE_FILE_SAVE);
    }

    /**
     * 文件删除队列
     *
     * @return
     */
    @Bean
    public Queue queueFileRemove() {
        return new Queue(QUEUE_FILE_REMOVE);
    }

    /**
     * 交换机
     *
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(FILE_EXCHANGE);
    }

    /**
     * 绑定文件保存队列到交换机
     *
     * @return
     */
    @Bean
    public Binding bindFileSave() {
        return BindingBuilder.bind(queueFileSave()).to(topicExchange()).with(KEY_FILE_SAVE);
    }

    /**
     * 绑定文件删除队列到交换机
     *
     * @return
     */
    @Bean
    public Binding bindFileRemove() {
        return BindingBuilder.bind(queueFileRemove()).to(topicExchange()).with(KEY_FILE_REMOVE);
    }

    /**
     * 定义延时队列
     *
     * @return
     */
    @Bean
    public Queue delayQueue() {
        //设置死信交换机和路由key
        return QueueBuilder.durable(DELAY_QUEUE)
                //如果消息过时，则会被投递到当前对应的死信交换机
                .withArgument("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE)
                //如果消息过时，死信交换机会根据routing-key投递消息到对应的队列
                .withArgument("x-dead-letter-routing-key", KEY_FILE_DEAD_LETTER)
                .build();
    }

    /**
     * 定义死信交换机
     *
     * @return
     */
    @Bean
    public TopicExchange deadLetterExchange() {
        return new TopicExchange(DEAD_LETTER_EXCHANGE);
    }

    /**
     * 定义死信队列
     *
     * @return
     */
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DEAD_LETTER_QUEUE);
    }

    /**
     * 绑定死信队列到死信交换机
     *
     * @return
     */
    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with(KEY_FILE_DEAD_LETTER);
    }
}
